/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clases;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;


/**
 *
 * @author Ele
 */
public class consultas {
 
     
 public void Conectar(){
        try{
            mdbc = new conexion();
            mdbc.init();
            Connection conn= mdbc.getMyConnection();
            stmt = conn.createStatement();
        }catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null, "Problemas al concetarse a la Base de Datos","Error",JOptionPane.ERROR_MESSAGE);
        }
    }      
  public void ListarPeso(JTable tablaPeso,int idTag){
     
            DefaultTableModel modelo = new DefaultTableModel();
            tablaPeso.setModel(modelo);
            tablaPeso.setAutoResizeMode(tablaPeso.AUTO_RESIZE_ALL_COLUMNS);
            
            int tam = getRowConsultaPeso(idTag);
            
             Object[][] m=new Object[tam][tablaPeso.getColumnCount()];         

           
            
            
             tablaPeso.setModel(new javax.swing.table.DefaultTableModel(m,
            new String [] {
                "Peso", "Fecha", "Hora"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
            
            
            
            
            try 
            { 
            
            Conectar();
            ResultSet rs = stmt.executeQuery("SELECT dl.peso,l.fecha, l.hora from detalle_lectura dl, bovino b, lectura l where b.idSenasa=dl.idsenasa and b.idSenasa='"+idTag+"' and dl.idlectura=l.idlectura order by l.fecha");
            int j=0;    
                while(rs.next()&& j<tam){
                  
                              String peso = rs.getString("peso");
                              String fecha = rs.getString("fecha");
                              String hora = rs.getString("hora"); 

                              tablaPeso.setValueAt(peso,j,0);
                              tablaPeso.setValueAt(fecha,j,1);
                              tablaPeso.setValueAt(hora,j,2);
                
                
                j=j+1;
                       }
                }
        catch(Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage(), "Error aqui", JOptionPane.ERROR_MESSAGE); }

    }
    
  
 int getRowConsultaPeso(int idTag){
     int tam=0;
       try 
            { 
            
            Conectar();
            ResultSet rs = stmt.executeQuery("SELECT dl.peso,l.fecha, l.hora from detalle_lectura dl, bovino b, lectura l where b.idSenasa=dl.idsenasa and b.idSenasa='"+idTag+"' and dl.idlectura=l.idlectura order by l.fecha");
              rs.last();
                tam= rs.getRow();
 }   
    catch(Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage(), "Error aqui obtener rows", JOptionPane.ERROR_MESSAGE); }   
       return tam;
 }
 
  private conexion mdbc;
  private Statement stmt; 
}
